-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests.c
667 lines (581 loc) · 18 KB
/
tests.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
/*
* BRS-tester by Anders Sandahl 2023-2024
*
* License GPL 2.0
*
* tests.c: Functions for testing
*
*/
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
#include "hal.h"
#include "vector.h"
static void waitUserInput(bool wait)
{
if (wait) {
printf(" (Wait)");
getc(stdin);
} else {
printf("\n");
}
}
int tests_selfTest(void)
{
float voltage_ref;
int voltage_ref_ok = 0;
char *str = NULL;
hal_powerEnable(1);
hal_setDefault();
/*
* First measure reference voltage, it should be 1700 mV nominal
*/
if (hal_measureVoltageRef(&voltage_ref) < 0) {
return -1;
}
if (fabs(voltage_ref - 1700.0) < 15) {
voltage_ref_ok = 1;
}
printf("Reference voltage: %7.1f mV (1700.0 mV) [ %s ]\n", voltage_ref, voltage_ref_ok ? " OK " : "FAIL");
/*
* Loop through all PINS and test them, high, low and with load.
*/
for (int k = AA; k < LAST_PIN; k++){
enum pinFunction function;
pin_getFunction(k, &function);
pin_getName(k, &str);
if (function == PIN_POWER) {
printf("PIN %s, PIN_POWER [ -- ]\n", str);
} else {
int voltage_ok = 0;
int current_ok = 0;
float current;
int data_h, data_l;
float voltage_h, voltage_l;
pin_setFunction(k, PIN_OUTPUT);
/*
* SetA "1" and measure
*/
pin_setDataOut(k, 1);
pin_setMeasure(k, 1);
usleep(10000);
pin_getValue(k, &data_h);
hal_measureVoltage (&voltage_h);
/*
* Set "0" and measure
*/
pin_setDataOut(k, 0);
usleep(10000);
pin_getValue(k, &data_l);
hal_measureVoltage (&voltage_l);
/*
* Test to measure with load (check measurement resistance)
*/
hal_enableLoad(4, true);
usleep(10000);
hal_measureCurrent (¤t);
hal_enableLoad(0, false);
if ( fabs(voltage_l) < 10.0 && fabs(voltage_h + 3700.0 ) < 50.0) {
voltage_ok = 1;
}
if ( fabs(current + 4.0) < 0.1){
current_ok = 1;
}
printf("PIN %s, D '1':%d (%7.1f mV), D '0':%d (%7.1f mV) I -4 mA: %7.1f mA [ %s ]\n",
str, data_h, voltage_h, data_l, voltage_l, current, (!data_l && data_h && voltage_ok && current_ok) ? " OK " : "FAIL");
pin_setMeasure(k, 0);
pin_setFunction(k, PIN_DISABLED);
}
}
/*
* Test the programmable load. Use PIN AE. If this pin has failed
* the test above, then this test will fail.
*/
pin_setFunction(AE, PIN_OUTPUT);
pin_setDataOut(AE, 0);
pin_setMeasure(AE, 1);
for (int curr = 2, i = 0; curr < 128; curr = (curr << 1), i++) {
float current;
float current_error;
int current_ok = 0;
/*
* In an ideal world, grounding the load resistors would have given
* current 2, 4, 8, 16, 32 and 64 mA. However, the measurement resistor for current
* and RDS(on) in the FET end up in series when testing the loads.
*/
const float currents[] = {-1.9, -3.9, -7.7, -15.0, -28.2, -50.2};
hal_enableLoad(curr, true);
usleep(10000);
hal_measureCurrent (¤t);
current_error = fabs(currents[i] - current);
if ((current_error < fabs(currents[i] * 0.05)) ||
(current_error < 0.2)){
current_ok = 1;
}
printf("Load: %.2d Meas: (%7.1f mA) (%7.1f mA) [ %s ]\n",
curr * -1, current, currents[i], (current_ok) ? " OK " : "FAIL");
}
pin_setFunction(AE, PIN_DISABLED);
pin_setMeasure(AE, 0);
/*
* Test drive strength
*/
hal_enableLoad(32, true);
for (int pin = AD; pin < LAST_PIN; pin++) {
float current, voltage;
int result = 0;
if (pin == BA || pin == BB || pin == BC)
continue;
pin_setFunction(pin, PIN_OUTPUT);
pin_setDataOut(pin, 0);
pin_setMeasure(pin, 1);
usleep(100000);
hal_measureCurrent (¤t);
hal_measureVoltage (&voltage);
result = (fabs(current) > 28.0 && fabs(voltage) < 400.0);
printf("Drive strength: %.2d mA, meas: (%7.1f mA) (%7.1f mV) [ %s ]\n",
32, current, voltage, (result) ? " OK " : "FAIL");
pin_setDataOut(pin, 1);
pin_setMeasure(pin, 0);
pin_setFunction(pin, PIN_DISABLED);
}
hal_enableLoad(0, false);
hal_setDefault();
hal_powerEnable(0);
return 0;
}
int tests_setupBoard(struct config const *b_cfg)
{
char const *str = b_cfg->pin_def;
/* Setup string can look like this:
* pppiodiodiodiodiod------------------
*
* 'p' - power pin, do nothing
* 'i' - input pin on tested board
* 'o' - output pin on tested board
* 'O' - output pin on tested board, open collector
* 'd' - pull down net on tested board
* 'g' - pin should be grounded on tested board
* '-' - pin should not be used
*/
for (int pin = AA; pin < LAST_PIN; pin ++) {
switch (str[pin]) {
case 'p':
printf("p");
break;
case '-':
printf("-");
break;
case 'i':
/*
* NOTE: PIN_OUTPUT is from the testers view.
* The format string describes the board under
* test.
*/
printf("i");
pin_setFunction(pin, PIN_OUTPUT);
break;
case 'O':
/*
* Open collector need pull down.
*/
printf("O");
pin_setFunction(pin, PIN_INPUT);
pin_enablePullDown(pin, 1);
break;
case 'o':
/*
* NOTE: PIN_INPUT is from the testers view.
* The format string describes the board under
* test.
*/
printf("o");
pin_setFunction(pin, PIN_INPUT);
break;
case 'd':
/*
* NOTE: PIN_INPUT is from the testers view.
* The format string describes the board under
* test. Setup pull down as input???
*/
printf("d");
pin_setFunction(pin, PIN_INPUT);
break;
case 'g':
/*
* NOTE: Pin should be tied to ground during test
*/
printf("g");
pin_setFunction(pin, PIN_GND);
break;
default:
printf("ERROR: Format error\n");
return -1;
}
}
printf("\n\n");
return 0;
}
int tests_checkVoltages(struct config const *b_cfg)
{
char const *str = b_cfg->pin_def;
float voltage;
int retVal = 0;
/* Setup string can look like this:
* pppiodiodiodiodiod------------------
*
* 'p' - power pin, do nothing
* 'i' - input pin on tested board
* 'o' - output pin on tested board
* 'O' - output pin on tested board, open collector
* 'd' - pull down net on tested board
* 'g' - pin should be grounded on tested board
* '-' - pin should not be used
*/
for (int pin = AA; pin < LAST_PIN; pin ++) {
if (str[pin] == 'g') pin_setFunction(pin, PIN_GND);
}
for (int pin = AA; pin < LAST_PIN; pin ++) {
int voltage_ok = 0;
char *pinName;
pin_getName(pin, &pinName);
switch (str[pin]) {
case '-':
break;
case 'p':
case 'g':
case 'i':
printf("Pin: %s %c\n", pinName, str[pin]);
break;
case 'o':
case 'O':
case 'd':
/*
* Check that voltage are in valid range
*/
pin_setMeasure(pin, 1);
usleep(50000);
hal_measureVoltage(&voltage);
pin_setMeasure(pin, 0);
if ( voltage < 10.0 && voltage > -4000.0){
voltage_ok = 1;
}
/*
* Return -1 if any voltage is outside the accepted range. Testing should not continue
* to protect the tester.
*/
if (voltage_ok != 1){
retVal = -1;
}
printf("Pin: %s %c voltage %7.1f [ %s ]\n", pinName, str[pin], voltage, voltage_ok ? " OK " : "FAIL");
break;
default:
printf("ERROR: Format error\n");
return -1;
}
}
printf("\n");
return retVal;
}
int tests_checkPullDown(struct config const *b_cfg)
{
const char *str = b_cfg->pin_def;
float voltage_l;
float voltage_h;
float current;
/* Setup string can look like this:
* pppiodiodiodiodiod------------------
*
* 'p' - power pin, do nothing
* 'i' - input pin on tested board
* 'o' - output pin on tested board
* 'O' - output pin on tested board, open collector
* 'd' - pull down net on tested board
* 'g' - pin should be grounded on tested board
* '-' - pin should not be used
*/
for (int pin = AA; pin < LAST_PIN; pin ++) {
int voltage_ok = 0;
int current_ok = 0;
char *pinName;
pin_getName(pin, &pinName);
switch (str[pin]) {
case 'p':
case 'g':
case '-':
case 'i':
case 'o':
case 'O':
break;
case 'd':
/*
* Check that pull down works
*/
pin_setMeasure(pin, 1);
usleep(100000);
hal_measureVoltage(&voltage_l);
pin_setGnd(pin, 1);
usleep(100000);
hal_measureVoltage(&voltage_h);
hal_measureCurrent(¤t);
pin_setGnd(pin, 0);
pin_setMeasure(pin, 0);
/*
* Check voltage high and low against configuration
*/
if ( fabs(voltage_l - b_cfg->load_low) < b_cfg->load_low_margin &&
fabs(voltage_h - b_cfg->load_high) < b_cfg->load_high_margin){
voltage_ok = 1;
}
/*
* Check current against configuration
*/
if ( fabs(current - b_cfg->load_current) < b_cfg->load_current_margin) {
current_ok = 1;
}
printf("Pin: %s %c voltage %7.1f %7.1f %7.1f [ %s ]\n", pinName, str[pin], voltage_l, voltage_h, current, (voltage_ok && current_ok) ? " OK " : "FAIL");
break;
default:
printf("ERROR: Format error\n");
return -1;
}
}
printf("\n");
return 0;
}
int tests_checkLogic(struct config const *b_cfg, char *vector, bool singleStep)
{
char const *setup = b_cfg->pin_def;
bool test_failed = false;
/* Setup string can look like this:
* pppiodiodiodiodiod------------------
*
* 'p' - power pin, do nothing
* 'i' - input pin on tested board
* 'o' - output pin on tested board
* 'O' - output pin on tested board, open collector
* 'd' - pull down net on tested board
* 'g' - pin should be grounded on tested board
* '-' - pin should not be used
*/
for (int pin = AA; pin < LAST_PIN; pin++) {
switch (setup[pin]) {
case 'p':
case 'g':
case '-':
case 'd':
case 'o':
case 'O':
printf("%c", vector[pin]);
break;
case 'i':
/*
* Set output
*/
if (vector[pin] == 'T') {
pin_setDataOut(pin, 0);
} else {
pin_setDataOut(pin, vector[pin] - '0');
}
printf("%c", vector[pin]);
break;
default:
printf("ERROR: Format error\n");
return -1;
}
}
printf("\n");
/*
* Check for toggling pin
*/
for (int pin = AA; pin < LAST_PIN; pin ++) {
char *pinName;
pin_getName(pin, &pinName);
if (setup[pin] == 'i' && vector[pin] == 'T') {
pin_toggleData(pin, b_cfg->toggles);
printf("Toggle pin %s %d times\n", pinName, b_cfg->toggles);
}
}
for (int pin = AA; pin < LAST_PIN; pin ++) {
int data_in;
int data_ok=0;
switch (setup[pin]) {
case 'p':
case 'g':
case '-':
case 'i':
printf("-");
break;
case 'd':
case 'o':
case 'O':
/*
* Get data in. '-' in format string means "don't check".
*/
if (vector[pin] != '-') {
pin_getValue(pin, &data_in);
if (vector[pin] == ('0' + data_in)){
data_ok = 1;
} else {
test_failed = true;
}
printf("%c", data_ok ? '0' + data_in : 'F');
break;
} else {
printf("x");
}
break;
default:
printf("ERROR: Format error\n");
return -1;
}
}
printf(" [ %s ]", test_failed ? "FAIL" : " OK ");
waitUserInput(singleStep);
return 0;
}
int tests_checkDriveStrength(struct config const *b_cfg, char *vector, bool singleStep)
{
printf("%s\n", vector);
float voltage;
char *pinName;
char const *setup = b_cfg->pin_def;
bool test_run = false;
bool result;
/* Setup string can look like this:
* pppiodiodiodiodiod------------------
*
* 'p' - power pin, do nothing
* 'i' - input pin on tested board
* 'o' - output pin on tested board
* 'O' - output pin on tested board, open collector
* 'd' - pull down net on tested board
* 'g' - pin should be grounded on tested board
* '-' - pin should not be used
*/
for (int pin = AA; pin < LAST_PIN; pin++) {
/*
* Set the load, there could only be one 'L' per line. Assign the load
* to that pin.
*/
if (vector[pin] == 'L') {
pin_setMeasure(pin, 1);
pin_enablePullDown(pin, 0);
hal_enableLoad(b_cfg->output_drive_strength, true);
usleep(200000);
}
}
for (int pin = AA; pin < LAST_PIN; pin++) {
switch (setup[pin]) {
case 'p':
case 'g':
case '-':
case 'd':
case 'o':
case 'O':
break;
case 'i':
/*
* Set output
*/
if (vector[pin] == '0' || vector[pin] == '1') {
pin_setDataOut(pin, vector[pin] - '0');
}
break;
default:
printf("ERROR: Format error\n");
return -1;
}
}
for (int pin = AA; pin < LAST_PIN; pin++) {
if (vector[pin] == 'L') {
usleep(10000);
pin_getName(pin, &pinName);
hal_measureVoltage(&voltage);
if (fabs(voltage - b_cfg->output_voltage_high) > b_cfg->output_voltage_margin){
result = false;
} else {
result = true;
}
/*
* Do some limit testing. Compare voltage
*/
printf("Pin %s %3d mA voltage %7.1f mV [ %s ]", pinName,
b_cfg->output_drive_strength,
voltage,
result ? " ok " : "fail");
waitUserInput(singleStep);
test_run = true;
/*
* Cleanup, reenable pull down if open collector.
*/
if (setup[pin] == 'O') {
pin_enablePullDown(pin, 1);
}
hal_enableLoad(0, false);
pin_setMeasure(pin, 0);
}
}
if (test_run) printf("\n");
return 0;
}
/*
* This function will loop over all inputs and verify that they don't
* draw to much current on the input pins.
*/
int tests_checkInputs(struct config const *b_cfg)
{
char const *setup = b_cfg->pin_def;
/* Setup string can look like this:
* pppiodiodiodiodiod------------------
*
* 'i' - input pin on tested board
*
* Just care about the inputs.
*/
for (int pin = AA; pin < LAST_PIN; pin++) {
if (setup[pin] == 'i') {
/*
* Set inactive level on all inputs
*/
pin_setDataOut(pin, 1);
}
}
for (int pin = AA; pin < LAST_PIN; pin++) {
if (setup[pin] == 'i') {
float current, voltage;
int result;
char *str;
pin_getName(pin, &str);
pin_setMeasure(pin, 1);
pin_setDataOut(pin, 1);
usleep(200000);
hal_measureCurrent(¤t);
hal_measureVoltage(&voltage);
if ((fabs(voltage - b_cfg->input_voltage_low) > 100) ||
(fabs(current - b_cfg->input_current_low) > b_cfg->input_current_margin)){
result = 0;
} else {
result = 1;
}
printf("Pin: %s 0 voltage %7.1f current %7.1f [ %s ]\n", str, voltage, current, result ? " OK " : "FAIL");
pin_setDataOut(pin, 0);
usleep(100000);
hal_measureCurrent(¤t);
hal_measureVoltage(&voltage);
if ((fabs(voltage - b_cfg->input_voltage_high) > 100) ||
(fabs(current - b_cfg->input_current_high) > b_cfg->input_current_margin)){
result = 0;
} else {
result = 1;
}
printf("Pin: %s 1 voltage %7.1f current %7.1f [ %s ]\n", str, voltage, current, result ? " OK " : "FAIL");
pin_setMeasure(pin, 0);
pin_setDataOut(pin, 0);
}
}
printf("\n");
return 0;
}