-
Notifications
You must be signed in to change notification settings - Fork 1
/
iicmaster.c
868 lines (816 loc) · 20.4 KB
/
iicmaster.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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
/*
* iicmaster is a low-level I2C test and debug tool for IBM internal use only
*/
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include "iicmaster.h"
#include <getopt.h>
#define IICMASTER_MAX_CMD_FILE_SZ 2048
#define DEBUG( fmt, args... ) if(dbg) printf("iicmaster: "fmt, ## args)
int dbg = 0;
char* tmpstr = 0;
int buf_len = 0;
char *buf = 0;
char *cl_data = 0;
int filed = -1;
int cmdfiled = -1;
char *bus = 0;
int scan = 0;
int scan_dbg = 0;
int display = 1;
int repeat = 0;
adal_iic_lock_t lock;
adal_iic_opts_t g_opts =
{
.xfr_opts = ADAL_IIC_XFR_DEFAULT,
.recovery = ADAL_IIC_REC_NONE,
};
enum
{
IIC_MIN_CMD = 0x0fff0000, /* keep adal ordinals from overalapping */
IIC_OPEN,
IIC_READ,
IIC_WRITE,
IIC_RESET,
IIC_DUMP,
IIC_LOCK,
IIC_LOCK_ENG,
IIC_CL_DATA,
IIC_FILE,
IIC_SET_TYPE,
IIC_SET_OPT,
IIC_SLOW,
IIC_FAST,
IIC_SPEED,
IIC_FLAG_NO_DMA,
IIC_FLAG_FORCE_DMA,
IIC_DELAY,
IIC_FLAG_SPECIAL_RD,
IIC_CMD_FILE,
IIC_VERBOSE,
IIC_DEBUG,
IIC_SCAN_WIDTH,
IIC_SCAN,
IIC_SCAN_DBG,
};
#define DEV_STR_SZ 32
struct type_conv
{
char str[DEV_STR_SZ];
adal_iic_opts_t dev_opts;
};
/* device type table */
struct type_conv dev_table[] = {
{
.str = "24C02",
.dev_opts.xfr_opts = ADAL_IIC_XFR_24C02,
.dev_opts.recovery = ADAL_IIC_REC_NONE,
},
{
.str = "24C04",
.dev_opts.xfr_opts = ADAL_IIC_XFR_24C04,
.dev_opts.recovery = ADAL_IIC_REC_NONE,
},
{
.str = "24C08",
.dev_opts.xfr_opts = ADAL_IIC_XFR_24C08,
.dev_opts.recovery = ADAL_IIC_REC_NONE,
},
{
.str = "24C16",
.dev_opts.xfr_opts = ADAL_IIC_XFR_24C16,
.dev_opts.recovery = ADAL_IIC_REC_NONE,
},
{
.str = "24C32",
.dev_opts.xfr_opts = ADAL_IIC_XFR_24C64,
.dev_opts.recovery = ADAL_IIC_REC_NONE,
},
{
.str = "24C64",
.dev_opts.xfr_opts = ADAL_IIC_XFR_24C64,
.dev_opts.recovery = ADAL_IIC_REC_NONE,
},
{
.str = "24C256",
.dev_opts.xfr_opts = ADAL_IIC_XFR_24C256,
.dev_opts.recovery = ADAL_IIC_REC_NONE,
},
{
.str = "24C512",
.dev_opts.xfr_opts = ADAL_IIC_XFR_24C512,
.dev_opts.recovery = ADAL_IIC_REC_NONE,
},
{
.str = "",
.dev_opts = {{0,},{0,}}
},
};
adal_iic_opts_t* dev_str2opts(char* str)
{
int i = 0;
adal_iic_opts_t* opts = 0;
if(!str || !str[0])
{
return 0;
}
while(dev_table[i].str[0] && strncmp(str, dev_table[i].str, DEV_STR_SZ))
{
i++;
}
if(dev_table[i].str[0])
{
opts = &dev_table[i].dev_opts;
}
return opts;
}
void sighandler(int sig)
{
printf("caught sig %d\n", sig);
}
struct sigaction act =
{
.sa_handler = sighandler,
};
int handle_cl_data(char* i_buf)
{
int count = strlen(i_buf);
char a;
int n;
int i;
buf_len = count/2;
buf = (char*)realloc(buf, buf_len);
for(i = 0; i < count; i++)
{
a = i_buf[i];
if(a >= '0' && a <= '9')
n = a - '0';
else if(a >= 'a' && a <= 'f')
n = a - 'a' + 10;
else if (a >= 'A' && a <= 'F')
n = a - 'A' + 10;
else{
printf("\nBad value in data string: '%c'\n", a);
return 1;
}
if(!(i%2))
{
buf[i/2] = n << 4;
}
else
{
buf[i/2] |= n;
}
}
return 0;
}
/* read up to sz bytes from opened file to buf */
void read_buf(int i_filed, char* buf, int sz)
{
int rc;
rc = lseek(i_filed, 0, SEEK_SET);
rc = read(i_filed, buf, sz);
}
/* write sz bytes from buf to opened file */
void write_buf(char* buf, int sz)
{
int rc;
rc = lseek(filed, 0, SEEK_SET);
rc = write(filed, buf, sz);
}
void display_buf(char* buf, int sz)
{
int n;
for(n = 0; n < sz; n++)
{
printf("%02X ", buf[n]);
if(!((n + 1) % 16))
{
printf("\n");
}
}
printf("\n");
}
int handle_file(const char * i_file, int * i_filed, unsigned long flags)
{
if(*i_filed >= 0)
close(*i_filed);
*i_filed = open(i_file, flags);
if(*i_filed < 0)
{
perror("iicmaster: file open failed");
printf("iicmaster: Could not open %s\n", i_file);
return 1;
}
return 0;
}
static void show_lck_msg(int cmd, char *bus, adal_iic_lock_t *lock, int lck_time)
{
if (cmd == IIC_LOCK_ENG) {
char *eng = strtok(bus, "P");
fprintf(stderr,
"!!The IIC engine %s"
" is %s (%ds)\n", eng, (lck_time)? "locked": "unlocked",
lck_time);
} else
fprintf(stderr,
"!!The IIC device"
" bus:%s addr:%x(0x%x)"
" is %s (%ds)\n",
bus,
lock->addr,
(lock->mask >> 1), (lck_time)? "locked": "unlocked",
lck_time);
}
int main(int argc, char * const *argv)
{
int cmd = 0;
int rc = 0;
int adal = 0;
int temp;
int adal_dormant = 0;
int adal_temp = 0;
int opt_index = 0;
char usg_str[2048];
char active_devstr[128];
char dormant_devstr[128];
char temp_devstr[128];
char *cmdbuf = 0;
char *const *cmd_argv;
int cmd_argc = 0;
int verbose = 0;
int slv_addr = 0;
int found = 0;
char found_list[128];
unsigned char sample_buf = 0;
int scan_speed = ADAL_IIC_VAL_100KHZ;
int scan_offset = 0;
int scan_width = 1;
int i;
const char *version = "2.12";
sprintf(usg_str, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
"[[OPTION]* [OPERATION]]*\n\n",
"iicmaster (pronounced ick-master) is a tool for performing low level\n",
"operations on an I2C bus. Multiple operations may be performed on a\n",
"single command line. Each specified option will only affect\n",
"operations that follow it on the command line. Since all operations\n",
"are done on a bus, a bus must be specified first, using the\n",
"-b <bus path> option, where <bus_path> has the following format:\n\n",
" /dev/iic/<link#>/<cfam#>/<eng#>/<port#>\n\nOPERATIONS\n",
"The special options, -r, -w, -u, -l, --reset, and --dump are\n",
"considered operations.\n\nOPTIONS\n",
"All other options are used to affect the behavior of the operations\n",
"that follow and do nothing by themselves. Options affect all\n",
"operations that follow until a new bus is specified or the option is\n",
"explicitly changed.\n\nEXAMPLES:\n",
"1) Write 0xDEADBEEF (4 bytes) to the device with address 0xA0.\n\n",
" iicmaster -b /dev/iic/0/0/14/0 -a 0xa0 --data deadbeef -w 4\n\n",
"2) Read 4 bytes from the device with address 0xA0.\n\n",
" iicmaster -b /dev/iic/0/0/14/0 -a 0xa0 -r 4\n\n",
"3) Write 0xDEADBEEF at offset 0 of device at 0xA0 and read it back.\n\n",
" iicmaster -b /dev/iic/0/0/14/0 -a 0xa0 -W 2 -o 0 --data deadbeef ",
"-w 4 -o 0 -r 4\n\n",
"4) Scan a bus for devices.\n\n",
" iicmaster -b /dev/iic/0/0/14/0 --scan\n\n",
"OPTIONS AND OPERATIONS\n");
sigaction(SIGINT, &act, 0);
struct option long_options[] = {
{ "reset", optional_argument, 0, IIC_RESET },
{ "bus", required_argument, 0, 'b' },
{ "delay", required_argument, 0, 'D' },
{ "read", required_argument, 0, 'r' },
{ "write", required_argument, 0, 'w' },
{ "lock", required_argument, 0, 'l' },
{ "lock_eng", required_argument, 0, IIC_LOCK_ENG },
{ "dev_addr", required_argument, 0, 'a' },
{ "dev_type", required_argument, 0, 't' },
{ "offset", required_argument, 0, 'o' },
{ "wdelay", required_argument, 0, 'd' },
{ "rdelay", required_argument, 0, 'y' },
{ "dev_width", required_argument, 0, 'W' },
{ "inc_addr", required_argument, 0, 'i' },
{ "slow", no_argument, 0, 's' },
{ "fast", no_argument, 0, 'f' },
{ "speed", required_argument, 0, IIC_SPEED },
{ "retry_pol", required_argument, 0, ADAL_IIC_CFG_REDO_POL },
{ "retry_delay", required_argument, 0, ADAL_IIC_CFG_REDO_DELAY },
{ "no_dma", no_argument, 0, IIC_FLAG_NO_DMA },
{ "force_dma", no_argument, 0, IIC_FLAG_FORCE_DMA },
{ "modified", no_argument, 0, 'M' },
{ "time", required_argument, 0, 'm' },
{ "rsplit", required_argument, 0, 'x' },
{ "wsplit", required_argument, 0, 'p' },
{ "repeat", required_argument, 0, 0 },
{ "display", required_argument, 0, 0 },
{ "data", required_argument, 0, IIC_CL_DATA },
{ "file", required_argument, 0, IIC_FILE },
{ "cmdfile", required_argument, 0, IIC_CMD_FILE },
{ "dump", no_argument, 0, IIC_DUMP },
{ "verbose", no_argument, 0, 'v' },
{ "debug", no_argument, 0, IIC_DEBUG },
{ "scan", no_argument, 0, IIC_SCAN },
{ "scanw", required_argument, 0, IIC_SCAN_WIDTH },
{ "scan_dbg", no_argument, 0, IIC_SCAN_DBG },
{ 0, 0, 0, 0 }
};
while((cmd = getopt_long(argc, argv, "b:D:r:w:l:a:t:o:d:y:W:i:sfm:x:p:v", long_options, &opt_index)) > 0)
{
if(optarg)
{
temp = strtoul(optarg, 0, 0);
}
if(!adal && (cmd != 'b' && cmd != IIC_CMD_FILE &&
cmd != 'v' && cmd != IIC_DEBUG))
{
printf("bus must be specified first\n");
exit(-1);
}
switch(cmd)
{
case 'b':
cmd = IIC_OPEN;
DEBUG("OPEN\n");
if(adal)
{
/* does new bus already have an open adal handle ? */
if(strcmp(active_devstr, optarg) == 0)
{
/* new bus is same as current adal handle so do nothing */
DEBUG("%s already open\n", active_devstr);
break;
}
else if(strcmp(dormant_devstr, optarg) == 0) {
/* new bus is same as dormant adal handle */
/* swap dormant & active states of open adal handles */
adal_temp = adal_dormant;
adal_dormant = adal;
adal = adal_temp;
strcpy(temp_devstr, dormant_devstr);
strcpy(dormant_devstr, active_devstr);
strcpy(active_devstr, temp_devstr);
DEBUG("%s dormant -> active\n", active_devstr);
break;
}
/* new bus does not have a currently open adal handle */
if(adal_dormant)
{
/* already two handles open so close the oldest one */
rc = close(adal_dormant);
DEBUG("%s closed\n", dormant_devstr);
}
adal_dormant = adal; /* previous active bus is now dormant */
strcpy(dormant_devstr, active_devstr);
DEBUG("%s dormant\n", dormant_devstr);
}
adal = open(optarg, O_RDWR | O_SYNC);
strcpy(active_devstr, optarg);
DEBUG("%s active\n", active_devstr);
if(verbose) printf("current bus: %s\n", active_devstr);
break;
case IIC_RESET:
if(optarg[0] == 'l')
{
DEBUG("RESET light\n");
rc = ioctl(adal, _IO(IIC_IOC_MAGIC, IIC_IOC_RESET_LIGHT), 0);
}
else if(optarg[0] == 'f')
{
DEBUG("RESET full\n");
rc = ioctl(adal, _IO(IIC_IOC_MAGIC, IIC_IOC_RESET_FULL), 0);
}
else
{
printf("Invalid Reset Type\n");
exit(-1);
}
if(rc < 0)
{
if (rc == -1)
printf("bus is stuck.\n");
else
perror("reset: adal_iic_reset "
"failed");
exit(-1);
}
if(verbose) printf("reset\n");
break;
case IIC_DUMP:
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, IIC_IOC_DISPLAY_REGS, long), 0);
if(rc)
{
perror("dump: adal_iic_config failed");
exit(-1);
}
if(verbose) printf("dump\n");
break;
case IIC_CL_DATA:
DEBUG("CL_DATA\n");
rc = handle_cl_data(optarg);
if(rc)
exit(-1);
break;
case IIC_FILE:
if(verbose) printf("open data file: %s\n", optarg);
rc = handle_file(optarg, &filed, O_RDWR | O_SYNC | O_CREAT);
if(rc)
exit(-1);
break;
case IIC_CMD_FILE:
if(verbose) printf("open command file: %s\n", optarg);
rc = handle_file(optarg, &cmdfiled, O_RDONLY | O_SYNC | O_CREAT);
if(rc)
exit(-1);
cmdbuf = (char*)realloc(cmdbuf, IICMASTER_MAX_CMD_FILE_SZ);
read_buf(cmdfiled, cmdbuf, IICMASTER_MAX_CMD_FILE_SZ);
DEBUG("command file string:\n%s\n", cmdbuf);
DEBUG("cmd file arg count:%d\n", cmd_argc);
break;
case 'r':
cmd = IIC_READ;
if(temp)
{
buf_len = temp;
}
buf = (char*)realloc(buf, buf_len);
if(buf_len && !buf)
{
printf("realloc failed\n");
exit(-1);
}
do{
rc = read(adal, buf, buf_len);
if(display && (rc > 0))
{
if(verbose)
{
printf("read :%s @%02x offset:%d = ",
active_devstr, g_opts.xfr_opts.dev_addr,
(int)g_opts.xfr_opts.offset);
}
display_buf(buf, rc);
}
if((filed >= 0) && (rc > 0))
{
write_buf(buf, rc);
}
} while(rc == buf_len && repeat--);
repeat = 0;
if(rc < 0)
{
perror("adal_iic_read failed");
exit(-1);
}
/* copy data to file if specified */
break;
case 'w':
cmd = IIC_WRITE;
if(temp)
{
buf_len = temp;
}
buf = (char*)realloc(buf, buf_len);
if(buf_len && !buf)
{
printf("realloc failed\n");
exit(-1);
}
if(filed >= 0)
{
read_buf(filed, buf, buf_len);
}
do{
rc = write(adal, buf, buf_len);
if(display && (rc > 0))
{
if(verbose)
{
printf("write data to %s @%02x offset:%d = ",
active_devstr, g_opts.xfr_opts.dev_addr,
(int)g_opts.xfr_opts.offset);
}
display_buf(buf, rc);
}
} while(rc == buf_len && repeat--);
repeat = 0;
if(rc < 0)
{
perror("adal_iic_write failed");
exit(-1);
}
repeat = 0;
break;
case 'l':
cmd = IIC_LOCK;
case IIC_LOCK_ENG:
lock.addr = g_opts.xfr_opts.dev_addr;
lock.mask = (cmd == IIC_LOCK_ENG)
? ADAL_IIC_VAL_LCK_ENG
: g_opts.xfr_opts.inc_addr;
lock.timeout = g_opts.xfr_opts.timeout;
if(verbose)
{
printf("lock %s @%02x mask:%08x\n",
active_devstr, g_opts.xfr_opts.dev_addr,
(unsigned int)g_opts.xfr_opts.inc_addr);
}
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, (cmd == IIC_LOCK_ENG) ? ADAL_IIC_CFG_LCK_ENG : ADAL_IIC_CFG_LCK_ADDR, long), &lock);
if(rc)
{
perror("lock: adal_iic_config failed");
exit(-1);
}
rc = daemon(0, 1);
if (rc < 0) {
rc = errno;
printf("daemoizing failed\n");
break;
}
show_lck_msg(cmd, bus, &lock, temp);
while (temp--) {
sleep(1);
/* show lck message every 5 seconds */
if (!(temp % 5) && temp)
show_lck_msg(cmd, bus, &lock,
temp);
}
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, (cmd == IIC_LOCK_ENG) ? ADAL_IIC_CFG_ULCK_ENG : ADAL_IIC_CFG_ULCK_ADDR, long), &lock);
if(rc)
{
perror("unlock: adal_iic_config failed");
exit(-1);
}
show_lck_msg(cmd, bus, &lock, 0);
break;
case 't':
cmd = IIC_SET_TYPE;
DEBUG("SET_TYPE\n");
{
adal_iic_opts_t *opts;
if(!(opts = dev_str2opts(optarg)))
{
printf("%s not supported\n",
optarg);
exit(-1);
}
memcpy(&g_opts, opts, sizeof(*opts));
}
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, ADAL_IIC_CFG_ALL, long), &g_opts);
if(rc)
{
perror("type: adal_iic_config failed");
exit(-1);
}
break;
case 's':
cmd = IIC_SLOW;
{
scan_speed = ADAL_IIC_VAL_100KHZ;
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, ADAL_IIC_CFG_SPEED, long), &scan_speed);
if(rc < 0)
{
perror("set slow failed");
exit(-1);
}
}
if(verbose) printf("set 100 KHz speed\n");
break;
case 'f':
cmd = IIC_FAST;
{
scan_speed = ADAL_IIC_VAL_400KHZ;
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, ADAL_IIC_CFG_SPEED, long), &scan_speed);
if(rc < 0)
{
perror("set fast failed");
exit(-1);
}
}
if(verbose) printf("set 400 KHz speed\n");
break;
case IIC_SPEED:
{
if((temp > 1000) || (temp < 1))
{
printf("invalid speed\n");
exit(-1);
}
scan_speed = temp;
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, ADAL_IIC_CFG_SPEED, long), &scan_speed);
if(rc < 0)
{
perror("set speed failed");
exit(-1);
}
printf("set %dKHz speed\n", rc);
}
break;
case IIC_FLAG_NO_DMA:
if(verbose) printf("Don't use DMA\n");
{
long flag = ADAL_IIC_FLAG_NO_DMA;
}
if(rc)
{
perror("set no dma failed");
exit(-1);
}
break;
case IIC_FLAG_FORCE_DMA:
if(verbose) printf("Force DMA\n");
{
long flag = ADAL_IIC_FLAG_FORCE_DMA;
}
if(rc)
{
perror("set force dma failed");
exit(-1);
}
break;
case 'M':
cmd = IIC_FLAG_SPECIAL_RD;
if(verbose) printf("special read mode\n");
{
long flag = ADAL_IIC_FLAG_SPECIAL_RD;
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, ADAL_IIC_CFG_FLAGS, long), &flag);
}
if(rc)
{
perror("set special read failed");
exit(-1);
}
break;
case IIC_DELAY:
if(verbose) printf("delay: %d\n", temp);
{
struct timespec ts = {
.tv_sec = temp/1000,
.tv_nsec =
(temp % 1000) * 1000000,
};
nanosleep(&ts, 0);
}
break;
case 'a':
cmd = ADAL_IIC_CFG_DEV_ADDR;
if(verbose) printf("set address: %02x\n", temp);
g_opts.xfr_opts.dev_addr = temp;
break;
case 'W':
cmd = ADAL_IIC_CFG_DEV_WIDTH;
if(verbose) printf("set width: %08x\n", temp);
g_opts.xfr_opts.dev_width = temp;
break;
case 'o':
cmd = ADAL_IIC_CFG_OFFSET;
if(verbose) printf("set offset: %d\n", temp);
g_opts.xfr_opts.offset = temp;
break;
case 'i':
cmd = ADAL_IIC_CFG_INC_ADDR;
if(verbose) printf("increment address: %d\n", temp);
g_opts.xfr_opts.inc_addr = temp;
break;
case 'm':
cmd = ADAL_IIC_CFG_TIMEOUT;
if(verbose) printf("set timeout: %d milliseconds\n", temp);
g_opts.xfr_opts.timeout = temp;
break;
case 'y':
cmd = ADAL_IIC_CFG_RDELAY;
if(verbose) printf("set read delay: %d milliseconds\n", temp);
g_opts.xfr_opts.rdelay = temp;
break;
case 'd':
cmd = ADAL_IIC_CFG_WDELAY;
if(verbose) printf("set write delay: %d milliseconds\n", temp);
g_opts.xfr_opts.wdelay = temp;
break;
case 'x':
cmd = ADAL_IIC_CFG_RSPLIT;
if(verbose) printf("set rsplit: %d\n", temp);
g_opts.xfr_opts.rsplit = temp;
break;
case 'p':
cmd = ADAL_IIC_CFG_WSPLIT;
if(verbose) printf("set wsplit: %d\n", temp);
g_opts.xfr_opts.wsplit = temp;
break;
case ADAL_IIC_CFG_REDO_POL:
if(verbose) printf("set retry policy: %d\n", temp);
g_opts.recovery.redo_pol = temp;
break;
case ADAL_IIC_CFG_REDO_DELAY:
if(verbose) printf("set retry delay: %d milliseconds\n", temp);
g_opts.recovery.redo_delay = temp;
break;
case 'v':
cmd = IIC_VERBOSE;
DEBUG("VERBOSE\n");
verbose = 1;
break;
case IIC_DEBUG:
dbg = temp;
DEBUG("version %s debug:%d\n", version, temp);
break;
case IIC_SCAN_WIDTH:
if (verbose) printf("set scan offset width: %d bytes\n", temp);
scan_width = temp;
break;
case IIC_SCAN_DBG:
scan_dbg = 1;
case IIC_SCAN:
scan = 1;
memset(found_list, 0, 128);
printf("Scanning for devices on bus:");
errno = 0;
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, ADAL_IIC_CFG_DEV_WIDTH, long), &scan_width);
if(rc < 0)
{
printf("\nerror setting config width, errno=%d\n", errno);
}
errno = 0;
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, ADAL_IIC_CFG_SPEED, long), &scan_speed);
if(rc < 0)
{
printf("\nerror setting config speed, errno=%d\n", errno);
}
errno = 0;
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, ADAL_IIC_CFG_OFFSET, long), &scan_offset);
if(rc < 0)
{
printf("\nerror setting config offset, errno=%d\n", errno);
}
/* walk each address on the bus and look for ACK's */
for(slv_addr = 0; slv_addr < 0x100; slv_addr += 2) {
if((slv_addr % 0x80) == 0) {
printf("\n");
} else {
printf(".");
}
errno = 0;
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, ADAL_IIC_CFG_DEV_ADDR, long), &slv_addr);
if(rc < 0)
{
printf("error setting config slave address, addr:0x%02x errno=%d\n",
slv_addr, errno);
}
errno = 0;
rc = read(adal, &sample_buf, 1);
if((errno == EALREADY) ||
((scan_dbg) && (errno == ETIME)))
{
/* can't scan the bus, something wrong with the clock
and/or data line */
scan = 0;
exit(-1);
}
if ((!scan_dbg) && (errno == ETIME)) {
ioctl(adal, _IO(IIC_IOC_MAGIC, IIC_IOC_RESET_LIGHT), 0);
} else if(errno != ENXIO) {
found_list[found++] = slv_addr;
}
}
printf("\nFound %d device(s):\n[ ", found);
for(i = 0; i < found; i++) {
printf("%02X ", found_list[i]);
}
printf("]\n");
break;
default:
printf("iicmaster: Unsupported command %d\n", cmd);
exit(-1);
}
if(cmd < IIC_MIN_CMD)
{
rc = ioctl(adal, _IOW(IIC_IOC_MAGIC, cmd, long), &temp);
if(rc)
{
perror("adal_iic_config failed");
printf("cmd=%08x, val=%08x\n",
cmd, temp);
exit(-1);
}
}
tmpstr = 0;
} // end while
if(cmd < -1)
{
if(buf) free(buf);
if(cmdbuf) free(cmdbuf);
exit(-1);
}
else
{
if(buf) free(buf);
if(cmdbuf) free(cmdbuf);
printf("iicmaster: Done.\n");
exit(0);
}
}